import { defineStore } from 'pinia'
import request from '@/utils/request'

export const useResourceStore = defineStore('resource', {
  state: () => ({
    resources: [],
    categories: [],
    loading: false,
    total: 0
  }),

  actions: {
    async fetchResources(page = 1, limit = 10, category = '') {
      this.loading = true
      try {
        // 模拟API请求
        const mockData = {
          list: [
            {
              id: 1,
              title: 'Visual Studio Code',
              description: '强大的代码编辑器',
              category: '开发工具',
              downloads: 1000,
              version: '1.80.0'
            },
            {
              id: 2,
              title: 'Postman',
              description: 'API开发测试工具',
              category: '开发工具',
              downloads: 800,
              version: '10.16.0'
            }
          ],
          total: 50
        }
        
        this.resources = mockData.list
        this.total = mockData.total
      } catch (error) {
        console.error('获取资源列表失败：', error)
      } finally {
        this.loading = false
      }
    },

    async fetchCategories() {
      try {
        // 模拟API请求
        this.categories = [
          { id: 1, name: '开发工具' },
          { id: 2, name: '设计工具' },
          { id: 3, name: '效率工具' },
          { id: 4, name: '系统工具' }
        ]
      } catch (error) {
        console.error('获取分类列表失败：', error)
      }
    }
  }
}) 